home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ddj0290.arc / BERGMAN.LST next >
File List  |  1990-01-07  |  7KB  |  350 lines

  1. _C_TALK/VIEWS_
  2. by Noel Bergman
  3.  
  4.  
  5.  
  6. [LISTING ONE]
  7.  
  8. !SketchApp subclass of: Object !
  9. className: SketchApp
  10. superClass: Object
  11. header:/* Class name: SketchApp */
  12. /* Super name: Object */
  13. /* File  name: Sketchpp */
  14. /* header */
  15. #include "objtypes.h"
  16. extern id Object;
  17. extern id Archiver;
  18. extern id Notifier;
  19. classVariables:/* class variables */instanceVariables:id objects;
  20. id filename;     /* Filename string */
  21. int changed;     /* changed flag */
  22. fileName:Sketchpp !
  23.  
  24. !SketchApp class methods !
  25.  
  26. !SketchApp instance methods !
  27.  
  28. add_ drawObj
  29. id drawObj;
  30. {
  31.     @self->objects add_ drawObj@;     /* add to collection */
  32.     @self changed@;
  33. }
  34. !
  35.  
  36. asCollect
  37. {
  38.   return self->objects;
  39. }
  40. !
  41.  
  42. changed
  43. {
  44.   self->changed = TRUE;
  45. }
  46. !
  47.  
  48. clear
  49. /* Erase the objects in our collection.  */
  50. {
  51.   extern id OrdCollect;
  52.  
  53.   if (self->filename) self->filename = @self->filename free@;
  54.   if (self->objects) @self->objects freeAll@;
  55.   self->objects = @OrdCollect new@;
  56.   @self changed@;
  57.   if (self->objects) return TRUE; else return FALSE;
  58. }
  59. !
  60.  
  61. getFname
  62. {
  63.   return self->filename;
  64. }
  65. !
  66.  
  67. loadFrom_ fname
  68. id fname;
  69. {
  70.    id fstr, arc;
  71.  
  72.    if (fname)
  73.    {
  74.       arc = @Archiver openForLoad_ fname@;
  75.       if (arc)
  76.       {
  77.          if (@self clear@) @self->objects freeAll@;
  78.          self->objects = @arc getObject@;
  79.          @self changed@;
  80.          @arc free@;
  81.          self->filename = fname;
  82.          return TRUE;
  83.       }
  84.    }
  85.    return FALSE;
  86. }
  87. !
  88.  
  89. main
  90. {
  91. }
  92.  
  93. main()
  94. {
  95.    id aView, aModel;
  96.    extern id SketchView;
  97.  
  98.    aModel = @SketchApp new@;
  99.    aView = @SketchView create_ "Sketch" ViewOf_ aModel@;
  100.    if (aView)
  101.    {
  102.       @aView show@;
  103.       @Notifier start@;
  104.    }
  105. }
  106.  
  107. !
  108.  
  109. save
  110. {
  111.    id arc;
  112.  
  113.    if (arc = @Archiver openForStore_ self->filename@)
  114.     {
  115.       @Notifier beginWait@;
  116.       @arc putObject_ self->objects@;
  117.       @arc free@;
  118.       self->changed = FALSE;
  119.       @Notifier endWait@;
  120.     }
  121. }
  122. !
  123.  
  124. saveAs_ filename
  125. id filename;
  126. {
  127.    if(self->filename) @self->filename free@;
  128.    self->filename = filename;
  129.    @self save@;
  130. }
  131.  
  132. !
  133.  
  134.  
  135.  
  136.  
  137. [LISTING TWO]
  138.  
  139.  
  140. *** New PointArray methods:
  141.  
  142. getFrom_ anArchive
  143. /* restore the receiver's size, recSize and size objects from the archive. */
  144. id anArchive;     /* id of an Archiver object to store the receiver to */
  145. {
  146.    id anId;
  147.    unsigned i;
  148.  
  149.    @super getFrom_ anArchive@;
  150.    self->howMany = @anArchive getInt@;
  151. }
  152.  
  153. putTo_ anArchive
  154. /* Store the receiver's size, recSize and size objects to the archive. */
  155. id anArchive;     /* id of an Archiver object to store the receiver to */
  156. {
  157.    id anId;
  158.    unsigned i;
  159.  
  160.    @super putTo_ anArchive@;
  161.    @anArchive putInt_ self->howMany@;
  162. }
  163.  
  164.  
  165. *** New Rect methods:
  166.  
  167. getFrom_ anArchiver
  168. {
  169.   self->left   = @anArchiver getInt@;
  170.   self->top    = @anArchiver getInt@;
  171.   self->right  = @anArchiver getInt@;
  172.   self->bottom = @anArchiver getInt@;
  173. }
  174.  
  175. putTo_ anArchiver
  176. {
  177.   @anArchiver putInt_ self->left@;
  178.   @anArchiver putInt_ self->top@;
  179.   @anArchiver putInt_ self->right@;
  180.   @anArchiver putInt_ self->bottom@;
  181. }
  182.  
  183.  
  184. *** New PolyLine methods:
  185.  
  186. getFrom_ anArchiver
  187. {
  188.   self->ptArray = @anArchiver getObject@;
  189. }
  190.  
  191. putTo_ anArchiver
  192. {
  193.   @anArchiver putObject_ self->ptArray@;
  194. }
  195.  
  196.  
  197. *** New Line methods:
  198.  
  199. getFrom_ anArchiver
  200. {
  201.   self->x0 = @anArchiver getInt@;
  202.   self->y0 = @anArchiver getInt@;
  203.   self->x1 = @anArchiver getInt@;
  204.   self->y1 = @anArchiver getInt@;
  205. }
  206.  
  207. putTo_ anArchiver
  208. {
  209.   @anArchiver putInt_ self->x0@;
  210.   @anArchiver putInt_ self->y0@;
  211.   @anArchiver putInt_ self->x1@;
  212.   @anArchiver putInt_ self->y1@;
  213. }
  214.  
  215.  
  216. *** New Box methods:
  217.  
  218. getFrom_ anArchiver
  219. {
  220.   self->rec = @anArchiver getObject@;
  221. }
  222.  
  223. putTo_ anArchiver
  224. {
  225.   @anArchiver putObject_ self->rec@;
  226. }
  227.  
  228. *** New or changed SketchView methods:
  229. *
  230. * You must add external ids for SketchApp, String, and FileSelect to the
  231. * SketchView class.  Also remove the instance variable "objects" and the
  232. * main method.
  233. *
  234.  
  235. erase
  236. /* Erase the objects drawn on the receiver.  */
  237. {
  238.   if (self->model)
  239.     {
  240.      @self->model clear@;
  241.      @self update@;
  242.     }
  243. }
  244.  
  245.  
  246. initialize
  247. /* Initialize receiver by creating a port to scribble on.   */
  248. {
  249.   id     aMenu;
  250.   extern id    Port, Menu, PopupMenu;
  251.   extern char  *scribNmes[];
  252.   extern int   scribSels[], scribKeys[];
  253.  
  254.   if (aMenu = @Menu new@)
  255.    {
  256.     if (self->fMenu = @PopupMenu new_ "&Functions"@)
  257.      {
  258.       @self->fMenu setNames_ scribNmes sels_ scribSels keys_ scribKeys for_ self@;
  259.       @aMenu append_ self->fMenu@;
  260.       @self setMenu_ aMenu@; 
  261.       if (@self->model clear@)
  262.        if (self->aPort = @Port createOn_ self@)  /* create graphics port on receiver */
  263.          return self;
  264.      }
  265.    }
  266.  return NIL;
  267. }
  268.  
  269. char  *scribNmes[] = {"S&ketch\tF1", "&Line\tF2", "&Box\tF3", "E&rase\tF4", "&Save\tF5", "Save&As\tF6", "&Open\tF7", NULL};
  270. int   scribKeys[]  = { K_F1,          K_F2,        K_F3,       K_F4,         K_F5,        K_F6,          K_F7,     K_NULL};
  271. int   scribSels[]  = {`sketch_`,     `line_`,     `box_`,     `erase`,      `save`,      `saveAs`,      `open`,         0};
  272.  
  273.  
  274. mouseDnX_ x Y_ y 
  275. /* Method to respond to mouse DOWN events.    */
  276. int x,y;    /* position in receiver where mouse down event occured  */
  277. {
  278.   if (self->drawClass)
  279.    {
  280.     @Notifier captureMouseFor_ self@; /* capture all mouse events to this window */
  281.     @Notifier mouseTrkOn@;            /* enable mouse traking => mouse movement events */
  282.  
  283.     self->drawObj = @self->drawClass new@;  /* new instance of draw object */
  284.     @self->model add_ self->drawObj@;     /* add to model */
  285.     @self->drawObj setPort_ self->aPort@;
  286.  
  287.     return @self->drawObj mouseDnX_ x Y_ y@; /* pass forward mouse event */
  288.    } 
  289.   else return FALSE;
  290.  
  291. open
  292. {
  293.    id fstr, str;
  294.  
  295.    fstr = @String newCStr_ "*.skh"@;
  296.    str = @FileSelect ask_ "Select a Sketch file" for_ fstr of_ self@;
  297.    if (str)
  298.    {
  299.       if (!!@self->model loadFrom_ str@) @str free@;
  300.       @self update@;
  301.    }
  302.    @fstr free@;
  303. }
  304.  
  305. paint
  306. /* Draw all graphic objects contained by receiver in the objects ordered  */
  307. /* collection.                                                             */
  308. {
  309.   int  sel = `drawOn_`;
  310.   id retainedObjs;
  311.  
  312.   if (retainedObjs = @self->model asCollect@)
  313.     @retainedObjs do_ sel with_ self->aPort@;
  314.  
  315.   return TRUE; 
  316. }
  317.  
  318. save
  319. {
  320.    if (@self->model getFname@) @self->model save@;
  321.    else @self saveAs@;
  322. }
  323.  
  324. saveAs
  325. {
  326.    id  fstr, str;
  327.  
  328.    fstr = @String newCStr_ "*.skh"@;
  329.    str = @FileSelect ask_ "Select a Sketch file" for_ fstr of_ self@;
  330.    if (str)
  331.      {
  332.       @self->model saveAs_ str@;
  333.      }
  334.    @fstr free@;
  335. }
  336.  
  337.  
  338. Example 1:  A generic shape method
  339.  
  340. *** Generic "shape" method for SketchView ***
  341. <shape>_ aMenuItem
  342. id  aMenuItem;  /* id of selected menu item */
  343. {
  344.   @self->fMenu uncheckAll@; 
  345.   self->drawClass = <ShapeClass>;
  346.   @aMenuItem check@; 
  347. }
  348.  
  349.